const mongoose = require('mongoose');
//https://www.cnblogs.com/xiaohuochai/p/7215067.html?utm_source=itdadao&utm_medium=referral
module.exports= class BaseRequest{
	constructor(modelname) {
		require('./schema/'+modelname);
		this.model = mongoose.model(modelname);
		console.log(this.module)
    }
    reject(err){
    	return {errorCode:10,message:err.message}
    }
    resolve(data){
    	return {
    		errorCode:0,data:data
    	}
    }
    save(body){
    	return new Promise((resolve,reject)=>{
    		const model=new this.model(body);
    		model.save((err,doc)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(doc))
    			}
    		})
    	})
    }
    insertMany(docs){
    	return new Promise((resolve,reject)=>{
    		this.model.insertMany(docs).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    find(condition,projection={},options={}){	
    	return new Promise((resolve,reject)=>{
    		this.model.find(condition,projection,options).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    findById(id,projection={},options={}){
    	return new Promise((resolve,reject)=>{
    		this.model.findById(id,projection,options).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    findOne(condition,projection={},options={}){
    	return new Promise((resolve,reject)=>{
    		this.model.findOne(condition,projection,options).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    update(condition, doc, options = {}){
    	return new Promise((resolve,reject)=>{
    		this.model.update(condition,doc,options).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    updateOne(condition, doc){
    	return new Promise((resolve,reject)=>{
    		this.model.updateOne(condition,doc).exec((err,data)=>{
    			if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
    		})
    	})
    }
    count(condition = {}) {
        return new Promise((resolve, reject) => {
            this.model.count(condition).exec((err, data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
            })
        })
    }
    findOneAndRemove(condition) {
        return new Promise((resolve, reject) => {
            this.model.findOneAndRemove(condition).exec((err,data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
            })
        })
    }
    remove(condition) {
        return new Promise((resolve, reject) => {
            this.model.remove(condition).exec((err,data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve(data))
    			}
            })
        })
    }
    queryPage(condition = {}, {page = 1, limit = 10}) {
        return new Promise(async (resolve, reject) => {
            const totalResult = await this.count(condition);
			const skipFrom = page * limit - limit;
           
			//console.log(totalResult,skipFrom)
            this.model.find(condition).skip(skipFrom).limit(limit).exec((err, data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve({
    					list:data,
    					total:totalResult.data,
    					pageNo:page,
    					pageSize:limit
    				}))
    			}
                
            })
        })
	}
	//populate 'topicid'//key字段     
	//{path:key,select:'id ',model:'model'}
	//'topicid','name'
	queryPagePopulate(condition = {}, {page = 1, limit = 10},populate) {
        return new Promise(async (resolve, reject) => {
            const totalResult = await this.count(condition);
			const skipFrom = page * limit - limit;
			//console.log(totalResult,skipFrom)
            this.model.find(condition).populate(populate).skip(skipFrom).limit(limit).exec((err, data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve({
    					list:data,
    					total:totalResult.data,
    					pageNo:page,
    					pageSize:limit
    				}))
    			}
                
            })
        })
	}
	queryPagePopulatetwo(condition = {}, {page = 1, limit = 10},populate,populatetwo) {
        return new Promise(async (resolve, reject) => {
            const totalResult = await this.count(condition);
			const skipFrom = page * limit - limit;
			//console.log(totalResult,skipFrom)
            this.model.find(condition).populate(populate).populate(populatetwo).skip(skipFrom).limit(limit).exec((err, data) => {
                if(err){
    				reject(this.reject(err))
    			}else{
    				resolve(this.resolve({
    					list:data,
    					total:totalResult.data,
    					pageNo:page,
    					pageSize:limit
    				}))
    			}
                
            })
        })
    }


}